package com.tuling.jvm;

// Arthas调优工具准备
import java.util.HashSet;

public class Arthas {
    private static HashSet hashSet = new HashSet();

    public static void main(String[] args) {
        // 模拟CPU线程过高
        CPUHigh();
        // 模拟线程死锁
        deadThread();
        // 不断向hashset集合里添加数据
        addHashSetThread();
    }

    // 不断向hashset集合里添加数据
    public static void addHashSetThread(){
        new Thread(()->{
            int count = 0;
            while (true){
                try {
                    hashSet.add("count:"+count);
                    Thread.sleep(10000);
                    count++;
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    // 模拟CPU线程过高
    public static void CPUHigh(){
        new Thread(()->{
            while (true){

            }
        }).start();
    }

    // 模拟线程死锁
    public static void deadThread(){
        Object resourceA = new Object();
        Object resourceB = new Object();

        Thread threadA = new Thread(()->{
            synchronized (resourceA){
                try {
                    System.out.println(Thread.currentThread().getName()+"resourceA开始");
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()+"waiting get resourceB");
                synchronized (resourceB){
                    System.out.println("resourceA结束");
                }
            }
        });

        Thread threadB = new Thread(()->{
            synchronized (resourceB){
                try {
                    System.out.println(Thread.currentThread().getName()+"resourceB开始");
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()+"waiting get resourceA");
                synchronized (resourceA){
                    System.out.println(Thread.currentThread().getName()+"resourceA结束");
                }
            }
        });
        threadA.start();
        threadB.start();
    }
}
